﻿using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using xiushen.Domain;

namespace xiushen.Tests
{
    public class SiteReading
    {
        private XiushenRepository _repository = new XiushenRepository();
        public void BookList()
        {
            var url = new Uri("http://ns.foyuan.net/book/index.html");
            var content = Spider.getHTML3(url);
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            string bookDescription = @"(?<=<td class=""indextdintr"">)[\s\S]+(?=</td>)";

            StreamReader reader = new StreamReader(new MemoryStream(content), Encoding.Default);

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();

                if (line.Contains(@"td class=""bookname"""))
                {
                    var host = string.Empty;
                    var description = string.Empty;
                    var href = Regex.Match(line, pattern_Href);
                    host = string.Format("http://ns.foyuan.net/book/{0}", href);
                    line = reader.ReadLine();
                    if (line.Contains(@"td class=""indextdintr"""))
                    {
                        description = Regex.Match(line, bookDescription).Value;
                    }
                    CreateBook(host, "国学大家", description);

                }

            }


        }

        public void GuoXueShuKu()
        {
            var url = new Uri("http://lib.ecit.edu.cn/guoxue/");
            var content = Spider.getHTML3(url);
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            string bookDescription = @"(?<=<td class=""indextdintr"">)[\s\S]+(?=</td>)";

            StreamReader reader = new StreamReader(new MemoryStream(content), Encoding.Default);

            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();

                if (line.Contains(@"target=""_blank"">"))
                {
                    var host = string.Empty;
                    var description = string.Empty;
                    var href = Regex.Match(line, pattern_Href);
                    host = string.Format("http://lib.ecit.edu.cn/guoxue/{0}", href);


                    CreateBook(host, "国学大家", description);

                }

            }
        }
        public void CreateBookShuKu(string bhost, string category, string description)
        {
            var id = _repository.QueryAll<Category>().Where(r => r.Name == category).FirstOrDefault().ID;
            int bookId = 0;
            if (_repository.QueryAll<Book>().Count() > 0)
            {
                bookId = _repository.QueryAll<Book>().Max(r => r.ID);
            }
            var url = new Uri(bhost);
            var content = Spider.getHTML(url);
            string content_Href = @"(?<=<li>)[\s\S]+(?=</li>)";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            string title_pattern = @"(?<=<a[^<>]+/>?)[^""<>]+(?=[</a>])";
            string booktitle_pattern = @"(?<=<h1>)[\s\S]+(?=</h1>)";
            string content_pattern = @"(?<=<div id=""content"">)[^ul]+(?=</div>)";

            var booktitle = Regex.Match(content, booktitle_pattern);
            var book = new Book();
            book.Name = booktitle.Value;
            book.CategoryID = id;
            book.ID = ++bookId;
            book.Description = description;
            _repository.Insert(book);

            var col = Regex.Match(content, content_Href);
            int chapterId = 0;
            StringBuilder sb = new StringBuilder();
            var values = col.Value.Split(new[] { "</li>" }, StringSplitOptions.None);

            // Añadimos al stringbuilder cada uno de los resultados
            foreach (var match in values)
            {
                var href = Regex.Match(match, pattern_Href);
                var host = string.Format("http://ns.foyuan.net/book/{0}", href);
                var title = Regex.Match(match, title_pattern);
                var chapter = new Chapter();
                chapter.BookID = bookId;
                chapter.CategoryID = id;
                chapter.Name = title.Value;
                chapter.ID = ++chapterId;
                var chapterContent = Spider.getHTML(new Uri(host));
                chapter.Content = Regex.Match(chapterContent, content_pattern).Value;
                _repository.Insert(chapter);

            }
        }
        public void CreateBook(string bhost, string category, string description)
        {
            var id = _repository.QueryAll<Category>().Where(r => r.Name == category).FirstOrDefault().ID;
            int bookId = 0;
            if (_repository.QueryAll<Book>().Count() > 0)
            {
                bookId = _repository.QueryAll<Book>().Max(r => r.ID);
            }
            var url = new Uri(bhost);
            var content = Spider.getHTML(url);
            string content_Href = @"(?<=<li>)[\s\S]+(?=</li>)";
            string pattern_Href = @"(?<=<a[^<>]+href=""?)[^""<>]+(?=""?[^<>]*>)";

            string title_pattern = @"(?<=<a[^<>]+/>?)[^""<>]+(?=[</a>])";
            string booktitle_pattern = @"(?<=<h1>)[\s\S]+(?=</h1>)";
            string content_pattern = @"(?<=<div id=""content"">)[^ul]+(?=</div>)";

            var booktitle = Regex.Match(content, booktitle_pattern);
            var book = new Book();
            book.Name = booktitle.Value;
            book.Author = "南怀瑾";
            book.CategoryID = id;
            book.ID = ++bookId;
            book.Description = description;
            _repository.Insert(book);

            var col = Regex.Match(content, content_Href);
            int chapterId = 0;
            StringBuilder sb = new StringBuilder();
            var values = col.Value.Split(new[] { "</li>" }, StringSplitOptions.None);

            // Añadimos al stringbuilder cada uno de los resultados
            foreach (var match in values)
            {
                var href = Regex.Match(match, pattern_Href);
                var host = string.Format("http://ns.foyuan.net/book/{0}", href);
                var title = Regex.Match(match, title_pattern);
                var chapter = new Chapter();
                chapter.BookID = bookId;
                chapter.CategoryID = id;
                chapter.Name = title.Value;
                chapter.ID = ++chapterId;
                var chapterContent = Spider.getHTML(new Uri(host));
                chapter.Content = Regex.Match(chapterContent, content_pattern).Value;
                _repository.Insert(chapter);

            }
        }
      

    }
}